package cool.goodbai.service.impl;

import cool.goodbai.entity.Post;
import cool.goodbai.example.PostExample;
import cool.goodbai.handler.DataProxy;
import cool.goodbai.mapper.PostMapper;
import cool.goodbai.service.PostService;
import cool.goodbai.util.StringUtil;
import cool.goodbai.vo.PageLazyResult;
import cool.goodbai.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;

    @Autowired
    private DataProxy dataProxy;

    @Override
    public Post insert(Post post) {
        PostExample example = new PostExample();
        example.createCriteria().andNameEqualTo(post.getName());
        List<Post> posts = postMapper.selectByExample(example);
        if (posts.size() == 0) {
            dataProxy.preInsert(post);
            postMapper.insertSelective(post);
            return postMapper.selectByPrimaryKey(post.getId());
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public Post delete(Long id) {
        Post post = postMapper.selectByPrimaryKey(id);
        if (post != null) {
            postMapper.deleteByPrimaryKey(id);
            return post;
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public Post update(Post post) {
        Post old = postMapper.selectByPrimaryKey(post.getId());
        if (old != null) {
            dataProxy.preUpdate(post);
            postMapper.updateByPrimaryKeySelective(post);
            return post;
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public PageResult<List<Post>> select(Post post, Integer current, Integer pageSize) {
        PostExample example = new PostExample();
        PostExample.Criteria criteria = example.createCriteria();
        //用户名
        if (!Objects.isNull(post.getId())) {
            criteria.andIdEqualTo(post.getId());
        }
        //岗位名称
        if (StringUtil.isNotEmpty(post.getName())) {
            criteria.andNameLike("%" + post.getName() + "%");
        }
        //所属院部
        if (StringUtil.isNotEmpty(post.getCollege())) {
            criteria.andCollegeLike("%" + post.getCollege() + "%");
        }
        //职位描述
        if (StringUtil.isNotEmpty(post.getDescription())) {
            criteria.andDescriptionLike("%" + post.getDescription() + "%");
        }
        //职位要求
        if (StringUtil.isNotEmpty(post.getRequire())) {
            criteria.andRequireLike("%" + post.getRequire() + "%");
        }
        //学历
        if (StringUtil.isNotEmpty(post.getEducation())) {
            criteria.andEducationLike("%" + post.getEducation() + "%");
        }
        //学位
        if (StringUtil.isNotEmpty(post.getDegree())) {
            criteria.andDegreeLike("%" + post.getDegree() + "%");
        }
        //截止时间 开始时间
        if (!Objects.isNull(post.getStartTime()) && !Objects.isNull(post.getDeadline())) {
            criteria.andStartTimeGreaterThanOrEqualTo(post.getStartTime()).andDeadlineLessThanOrEqualTo(post.getDeadline());
        }
        //是否审核
        if (!Objects.isNull(post.getValid())) {
            criteria.andValidEqualTo(post.getValid());
        }

        long total = postMapper.countByExample(example);
        if (current != null && pageSize != null) {
            example.setLimit(pageSize);
            example.setOffset(((current.longValue() - 1) * pageSize));
        }
        List<Post> posts = postMapper.selectByExample(example);
        return new PageResult<>(posts, current, pageSize, true, total, 0);
    }

    @Override
    public PageLazyResult<List<Post>> selectLazy(Post post, Integer count) {
        PostExample example = new PostExample();
        PostExample.Criteria criteria = example.createCriteria();
        //岗位名称
        if (StringUtil.isNotEmpty(post.getName())) {
            criteria.andNameLike(post.getName());
        }
        //所属院部
        if (StringUtil.isNotEmpty(post.getCollege())) {
            criteria.andCollegeLike(post.getCollege());
        }
        //职位描述
        if (StringUtil.isNotEmpty(post.getDescription())) {
            criteria.andDescriptionLike(post.getDescription());
        }
        //职位要求
        if (StringUtil.isNotEmpty(post.getRequire())) {
            criteria.andRequireLike(post.getRequire());
        }
        //学历
        if (StringUtil.isNotEmpty(post.getEducation())) {
            criteria.andEducationLike(post.getEducation());
        }
        //学位
        if (StringUtil.isNotEmpty(post.getDegree())) {
            criteria.andDegreeLike(post.getDegree());
        }
        //截止时间 开始时间
        if (!Objects.isNull(post.getStartTime()) && !Objects.isNull(post.getDeadline())) {
            criteria.andStartTimeGreaterThanOrEqualTo(post.getStartTime()).andDeadlineLessThanOrEqualTo(post.getDeadline());
        }

        long total = postMapper.countByExample(example);
        if (count != null) {
            example.setLimit(5);
            example.setOffset((long) (count - 5));
        }
        List<Post> posts = postMapper.selectByExample(example);
        return new PageLazyResult<>(0, posts, 1, count, true, total);
    }
}
